//
//  MainNewsDetailsCell.m
//  MoXiProject
//
//  Created by 沈晨宇 on 2017/7/3.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "MainNewsDetailsCell.h"

@implementation MainNewsDetailsCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.iconImage=[UIImageView new];
        self.iconImage.backgroundColor=[UIColor redColor];
        self.iconImage.layer.cornerRadius = 25;
        self.iconImage.layer.masksToBounds = YES;
        [self.contentView addSubview:self.iconImage];
        
        self.name=[UILabel new];
        self.name.textColor=baseBlackColor;
        self.name.font=DEF_MyFont(16);
        self.name.numberOfLines=0;
        self.name.textAlignment=NSTextAlignmentLeft;
        [self.contentView addSubview:self.name];
        
        self.time=[UILabel new];
        self.time.textColor=baseLightBlackColor;
        self.time.font=DEF_MyFont(14);
        self.time.textAlignment=NSTextAlignmentLeft;
        [self.contentView addSubview:self.time];
        
        self.comments=[UILabel new];
        self.comments.textColor=baseLightBlackColor;
        self.comments.font=DEF_MyFont(14);
        self.comments.numberOfLines=0;
        self.comments.textAlignment=NSTextAlignmentLeft;
        [self.contentView addSubview:self.comments];
        
        self.report=[UIButton new];
        self.report.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14.f];
        [self.report setTitle:@"举报" forState:UIControlStateNormal];
        [self.report setTitleColor:baseLightBlackColor forState:UIControlStateNormal];
        [self.contentView addSubview:self.report];
        
        self.line=[UIView new];
        self.line.backgroundColor=DEF_COLOR_F0F0F0;
        [self.contentView addSubview:self.line];
        
    }
    
    return self;
}

-(void)setModel:(MainCommentsListModel *)model{

    _model=model;
    [self setFrame];
    [self setData];
}

-(void)setFrame{
    
    [self.iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(50);
        make.top.equalTo(self.contentView).with.offset(12);
        make.leading.equalTo(self.contentView).with.offset(15);
    }];
    
    [self.name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(16);
        make.top.equalTo(self.contentView).with.offset(12);
        make.leading.equalTo(self.iconImage.mas_trailing).with.offset(10);
    }];

    [self.time mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(14);
        make.top.equalTo(self.name.mas_bottom).with.offset(10);
        make.leading.equalTo(self.iconImage.mas_trailing).with.offset(10);
    }];
    

    [self.report mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(12);
        make.top.equalTo(self.contentView).with.offset(12);
        make.trailing.equalTo(self.contentView).with.offset(-15);
    }];

    NSString* comment=[self base64DecodeString:_model.content];
//    CGSize commentsSize=[comment boundingRectWithSize:CGSizeMake(DEF_DEVICE_WIDTH-30, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName : DEF_MyFont(14)} context:nil].size;
//    
//    [self.comments mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.mas_equalTo(DEF_DEVICE_WIDTH-30);
//        make.height.mas_equalTo(commentsSize.height+1);
//        make.top.equalTo(self.iconImage.mas_bottom).with.offset(5);
//        make.leading.equalTo(self.contentView).with.offset(15);
//    }];
    
    CGFloat commentsNumW = DEF_DEVICE_WIDTH-30;
    CGSize commentsSize=[comment boundingRectWithSize:CGSizeMake(commentsNumW, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName :DEF_MyFont(14) } context:nil].size;
    CGFloat commentsNumX =15;
    CGFloat commentsNumY = 67;
    CGFloat commentsNumH = commentsSize.height+1;
    self.comments.frame=CGRectMake(commentsNumX, commentsNumY, commentsNumW, commentsNumH);
    
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(DEF_DEVICE_WIDTH-15);
        make.height.mas_equalTo(1);
        make.top.equalTo(self.comments.mas_bottom).with.offset(12);
        make.leading.equalTo(self.contentView).with.offset(15);
    }];
    
    self.frame=CGRectMake(0, 0, 0, 50+12+5+commentsSize.height+1+13);
//    self.frame=CGRectMake(0, 0, 0, 50+12);
}

-(void)setData{
    
    NSRange range = NSMakeRange(5,11);
    NSString* dateStr= [_model.createTime substringWithRange:range];
    NSString* comment=[self base64DecodeString:_model.content];
    self.name.text=_model.username;
    self.time.text=dateStr;
    self.comments.text=comment;
    [self.iconImage sd_setImageWithURL:[NSURL URLWithString:_model.imgUrl]];

}
-(NSString *)base64DecodeString:(NSString *)string
{
    
    //1.将base64编码后的字符串『解码』为二进制数据
    
    NSData *data = [[NSData alloc]initWithBase64EncodedString:string options:0];
    
    //2.把二进制数据转换为字符串返回
    
    return [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    
}
@end
